<?php 
include 'inc/reuse/dbconfig.php';
// นับจำนวนหน้า
$per_page = 24;
$sql = "SELECT * FROM products ";
if($main_id != "")
	$sql .= "WHERE fix_main_products_type_id = $main_id ";
if($sub_id != "")
	$sql .= "AND fix_sub_products_type_id = $sub_id ";
if(isset($_GET['search']))
{
	if($main_id == "")			$sql .= "WHERE products_name LIKE '%".$_GET['search']."%' ";
	else 						$sql .= "AND products_name LIKE '%".$_GET['search']."%' ";	
}

$sql .= "ORDER BY products_id ";

$rsd = mysql_query($sql);
$numOfRow = mysql_num_rows($rsd);
$pages = ceil($numOfRow/$per_page);

//ดึงข้อมูลแบ่งเป็นหน้า
$start = ($page-1)*$per_page;
$sql .= "limit $start,$per_page";
$rsd = mysql_query($sql);
$numOfRow = mysql_num_rows($rsd);
$dirname = "images/uploads/";
$td_images = "";
$td_name = "";
$data = "";
$count = 1;
if($numOfRow > 0)
{	
	while($row = mysql_fetch_array($rsd)){
		//เก็บข้อมูล ลชื่อสินค้า และ รุปภาพสินค้า
		$images_dir = $dirname.$row['products_id'];
		if(is_dir($images_dir))
		{
			$images = scandir($images_dir);
			$ignore = Array(".", "..",".svn", "otherfiletoignore");
			foreach($images as $curimg){
				if(!in_array($curimg, $ignore)) {
					$td_images .= "<td valign=\"top\">
											<div id=\"product-border\">
						                    	<a href=\"products_detail.php?products_id=".$row['products_id']."\" target=\"_blank\"><img src=\"$images_dir/$curimg\"  /> </a> 
						                        <div class=\"product-detail\"><a href=\"products_detail.php?products_id=".$row['products_id']."\" target=\"_blank\">".$row['products_name']."</a></div>
							               </div>
									   </td>";
					break;
					//<a href=\"products_detail.php?products_id=".$row['products_id']."\" target=\"_blank\"><img src=\"$images_dir/$curimg\" width=\"150\" height=\"200\"></a>
				}
			}
		}
		if($count % 3 == 0 || $count == $numOfRow)
		{
			$data .= "<tr valign=\"top\">$td_images</tr>";
			$td_images = "";
		}
		$count++;
		
	}
	
	echo $data;
}else{
	echo "<tr valign=\"top\" ><td align=\"center\" width=700 style=\"padding-top:30px; \">ไม่มีสินค้าประเภทนี้</td></tr>";
}
?>